<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('Analyse')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>名称：</label>
                            <input type="text" name="requestName"/>
                        </li>
                        <li>
                            <label>状态：</label>
                            <select name="requestStatus" th:with="type=${@dict.getType('sys_request_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>创建者：</label>
                            <input type="text" name="requester"/>
                        </li>
                        <li class="select-time">
                            <label>申请时间： </label>
                            <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endTime]"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:request:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var analyseFlag = [[${@permission.hasPermi('system:analyse')}]];
    var requestStatusDatas = [[${@dict.getType('sys_request_status')}]];
    var prefix = ctx + "system/analyse";
    var prefix2 = ctx + "system/request";
    var analyseMsg = [[${analyseMsg}]];

    function onLoadSuccess(data){
        if(analyseMsg != null){
            alert(analyseMsg);
        }else {
            console.log("analyseMsg == null");
        }

        $.each(data.rows, function (index, value){
            if(value.length == 0){
                return;
            }
            $.each(value.services, function( key, value, index) {
                if(value.process == 11){ //Done
                    $("#serv" + value.serviceId +" i:first-child").css("color","green");
                    $("#serv" + value.serviceId +" i:first-child").removeClass();
                    $("#serv" + value.serviceId +" i:first-child").addClass("fa fa-check fa-lg")
                }else if(value.process == 12 || value.process == 1 ){ //Error、cancel
                    $("#serv" + value.serviceId +" i:first-child").css("color","red");
                    $("#serv" + value.serviceId +" i:first-child").removeClass();
                    $("#serv" + value.serviceId +" i:first-child").addClass("fa fa-close fa-lg")
                }else if(value.process == 2){
                    // $("#serv" + value.serviceId +" i:first-child").css("color","blue");
                    $("#serv" + value.serviceId +" i:first-child").removeClass();
                    $("#serv" + value.serviceId +" i:first-child").addClass("fa fa-hourglass-1 fa-lg")
                }else if(value.process != null && value.process != 0){ //Editable、Submitted
                    // $("#serv" + value.serviceId +" i:first-child").css("color","blue");
                    $("#serv" + value.serviceId +" i:first-child").removeClass();
                    $("#serv" + value.serviceId +" i:first-child").addClass("fa fa-spin fa-circle-o-notch fa-lg")
                }

            });
        });
    }

    $(function() {
        var options = {
            url: prefix + "/list",
            anaUrl: prefix + "/ana/{id}",
            viewUrl2: prefix2 + "/samview/{id}",
            viewUrl3: prefix2 + "/serview/{id}",
            detailUrl: prefix + "/detail/{id}",
            exportUrl: prefix2 + "/export",
            modalName: "Request",
            sortName: "createTime",
            sortOrder: "desc",
            onLoadSuccess: onLoadSuccess,
            columns: [{
                checkbox: true
            },
                {
                    field: 'requestId',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'requestName',
                    title: '服务请求名称',
                    // formatter: function (value, row, index) {
                    //     return '<a href="javascript:void(0)" onclick="$.operate.detailTab2(\'' + row.requestId + '\')">'+value+'</a>';
                    // }
                },
                {
                    field: 'samples'  ,
                    title: '样品',
                    formatter: function (value, item, index) {
                        var samples = [];
                        $.each(value,function (index, data) {
                            var s = JSON.stringify(data);
                            var parse = JSON.parse(s);
                            samples.push('<a href="javascript:;" onclick="$.operate.view2(\''+parse.sampleId+'\',\''+parse.sampleName+'\')"><i class="fa fa-flask"></i> '+parse.sampleName+'</a><br>')
                        })
                        return samples.join('');
                    }
                },
                {
                    field: 'services'  ,
                    title: '服务',
                    formatter: function (value, item, index) {
                        var services = [];
                        $.each(value,function (index, data) {
                            var s = JSON.stringify(data);
                            var parse = JSON.parse(s);
                            services.push('<a id="serv'+parse.serviceId+'" href="javascript:;" onclick="$.operate.view3(\''+parse.serviceId+'\' , \''+parse.serviceName+'\')"><i class="fa fa-adjust fa-lg"></i> '+parse.serviceName+' - '+parse.sampleName+'</a><br>')
                        })
                        return services.join('');
                    }
                },
                {
                    field: 'description',
                    title: '描述'
                },
                {
                    field: 'requestStatus',
                    title: '状态',
                    sortable: true,
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(requestStatusDatas, value);
                    }
                },
                {
                    field: 'createTime',
                    title: '申请时间',
                    sortable: true
                },
                {
                    field: 'completeness',
                    title: '完成度',
                    visible: false
                },
                {
                    field: 'userReq.userName',
                    title: '创建者'
                },
                {
                    field: 'userMag.userName',
                    title: 'AM',
                },
                {
                    field: 'userApr.userName',
                    title: 'QA',
                    visible: false
                },
                {
                    field: 'result',
                    title: '结果路径',
                    visible: false
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        var analyse = '<a class="btn btn-success btn-xs ' + analyseFlag + '" href="javascript:void(0)" onclick="$.operate.anaTab(\'' + row.requestId + '\')"><i class="fa fa-edit"></i>Analyse</a> ';
                        // actions.push('<a class="btn btn-info btn-xs ' + analyseFlag + '" href="javascript:void(0)" onclick="$.operate.detailUrl(\'' + row.requestId + '\')"><i class="fa fa-search"></i>Show Details</a> ');
                        var showDetails = '<a class="btn btn-info btn-xs ' + analyseFlag + '" href="javascript:void(0)" onclick="$.operate.anaTab(\'' + row.requestId + '\')"><i class="fa fa-search"></i>Show Details</a> ';
                        if(row.requestStatus == 8 ){
                            actions.push(showDetails);
                        }else {
                            actions.push(analyse);
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
</script>
</body>
</html>